//index.js
//获取应用实例
const app = getApp()

var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
var i = 0;
Page({
  data: {
    menuHeight:0,//主页下方菜品列表的高度
    storeImg: "../../libs/images/logo.png",//门店照片
    addr:'',//门店地址
    storeName:'义泰昌（车公庙店）',//门店名称
    price:25,//起送价
    psPrice:3,//配送费
    activeMsg:{//满减活动
      activeLogo:'满',
      activeTxt: '满20减1元  满50减3元  满100减10元'
    },
    coupon:{//优惠券
      couponP:50,
      couponArea:'全部门店可用'
    },
    //菜品数据
    menuRight:[
      { name: '热销', id: 'hot', li: [{ commodity: '卡布奇诺', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '蓝山咖啡', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 1, specArr: [{ specName: '规格', spec: [{ specType: '大份', specChoise: 0, specStyle: 1, specPrice: 45, specAdd: 0 }, { specType: '中份', specChoise: 0, specStyle: 0, specPrice: 35, specAdd: 0 }, { specType: '小份', specChoise: 0, specStyle: 0, specPrice: 25, specAdd: 0 }] }] }, { commodity: '猫屎咖啡', imgUrl: '../../libs/images/logo.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '雀巢咖啡', imgUrl: '../../libs/images/logo.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '速溶咖啡', imgUrl: '../../libs/images/logo.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }]},
      { name: '肉食热销热', id: 'meat', li: [{ commodity: '牛肉', imgUrl: '../../libs/images/logo.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 1, specArr: [{ specName: '规格', spec: [{ specType: '小份', specChoise: 0, specStyle: 1, specPrice: 30, specAdd: 0 }, { specType: '中份', specChoise: 0, specStyle: 0, specPrice: 40, specAdd: 0 }, { specType: '大份', specChoise: 0, specStyle: 0, specPrice: 50, specAdd: 0 }] }] }, { commodity: '猪肉', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '羊肉', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '鱼丸', imgUrl: '../../libs/images/logo.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }] },
      { name: '蔬菜', id: 'vag', li: [{ commodity: '生菜', imgUrl: '../../libs/images/logo.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 1 }, { commodity: '白菜', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '菠菜', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '西洋菜', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 1 }] },
      { name: '汤底', id: 'soup', li: [{ commodity: '麻辣', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '浓骨', imgUrl: '../../libs/images/b_n.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 0 }, { commodity: '鸳鸯锅', imgUrl: '../../libs/images/logo.png', saleNum: 362, dishPrice: 15.5, display: 0, buyNum: 0, isSpec: 1 }] }
    ],
    //购物车弹窗数据
    postData:{
      totalNum:0,
      dishList:[]
    },
    leftNum:0,//左边第几个分类
    rightNum:0,//右边第几个菜品
    specNum:0,//规格的第几个
    // display:0,
    specPopup:0,//是否显示选规格弹窗
    carPopup:0,//是否显示购物车弹窗
    menuStyle:0,//是否显示左下菜单栏
    noticeModal:{//自定义弹出框
      imgUrl:'',
      mainText:'',
      explainText:'',
      btnText:'',
      show:0
    },
    // 用于测试弹窗
    popupShow:0
  },
  onLoad: function (options) {
  
    qqmapsdk = new QQMapWX({
      key: '2C7BZ-2B3A6-OMDSG-E7JWG-MG33Z-PZBAJ'
    });


    var that = this;
    //获取现在所在位置
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            var address = addressRes.result.formatted_addresses.recommend;
            that.setData({
              addr: address
            })
          }
        })
      }
    })

    // wx.request({
    //   url: 'category/fetch',
    //   data: {
    //     stordId: this.data.storeName
    //   },
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //   }
    // })
    
    //设置好菜品列表的高度
    wx.getSystemInfo({
      success:function(res){
        console.log(res.windowHeight - res.windowWidth / 750 * 207)
        that.setData({
          menuHeight: res.windowHeight - res.windowWidth / 750 * 434
        })
      }
    })

    //模拟各种状态的弹窗，修改用于测试弹窗的popupShow的值可看见弹窗
    //店铺暂停接单(自定义弹窗)
    if (this.data.popupShow == 1){
      var modal = this.data.noticeModal;
      modal = {
        imgUrl: '../../libs/images/sorry.png',
        mainText: '抱歉，店铺暂停接单，请稍后再来',
        explainText: '',
        btnText: '确定',
        show: 1
      }
      this.setData({
        noticeModal: modal
      })
      //网络超时(自定义弹窗)
    } else if (this.data.popupShow == 2){
      var modal = this.data.noticeModal;
      modal = {
        imgUrl: '../../libs/images/notice.png',
        mainText: '网络超时，请重试',
        explainText: '',
        btnText: '重试',
        show: 1
      }
      this.setData({
        noticeModal: modal
      })
      //店铺已打烊(自定义弹窗)
    } else if (this.data.popupShow == 3) {
      var modal = this.data.noticeModal;
      modal = {
        imgUrl: '../../libs/images/closed.png',
        mainText: '店铺已打烊',
        explainText: '营业时间08:00-10:00 17:00-22:00',
        btnText: '确定',
        show: 1
      }
      this.setData({
        noticeModal: modal
      })
      //当前位置不在配送范围内(微信自带弹窗)
    } else if (this.data.popupShow == 4){
      wx.showModal({
        title: '当前位置不在配送范围内',
        cancelText:'修改地址',
        content: '',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击修改范围')
          }
        }
      })
    }

  },
  //点击自定义的弹窗中的按钮时
  noticePrimary:function(e){
    console.log('这是一个自定义的弹窗')
  },
  onReady: function () {
    // Do something when page ready.
  },
  onShow: function () {
    // Do something when page show.

  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  onPullDownRefresh: function () {
    // Do something when pull down.
  },
  onReachBottom: function () {
    // Do something when page reach bottom.
  },
  onShareAppMessage: function () {
    // return custom share data when user share.
  },
  onPageScroll: function () {
    // Do something when page scroll
  },
  // 点击左边分类列表
  tap:function(e){
    var j = parseInt(e.currentTarget.dataset.i);
    console.log(e)
    this.setData({
      leftNum:j
    })
  },
  onTabItemTap(item) {
    console.log(item.index)
    console.log(item.pagePath)
    console.log(item.text)
  },
  // 点击选择地址
  chooseAddr:function(){
    wx.navigateTo({
      url: '../main/shipAddress/shipAddress'
    })
  },
  // 点击切换门店
  storeSwitchTap:function(){
    wx.navigateTo({
      url: '../main/storeSwitch/storeSwitch'
    })
  },
  // 点击跳转查看门店信息
  storeMessage:function(){
    wx.navigateTo({
      url: '../main/storeMessage/storeMessage'
    })
  },
  // 点击跳转个人信息
  toUserMsg:function(){
    wx.navigateTo({
      url: '../user/userMsg/userMsg'
    })
  },
  // 点击跳转我的订单
  toMyOrder: function () {
    wx.navigateTo({
      url: '../order/myOrder/myOrder'
    })
  },
  // 点击右边菜品的加号
  addTap: function (e) {
    var dis = e.currentTarget.dataset.dis;
    var num = this.data.leftNum;
    var disVal = this.data.menuRight;
    disVal[num].li[dis].display= 1;
    disVal[num].li[dis].buyNum = 1;
    var postData = this.data.postData;
    postData.totalNum += 1;
    var dishList = {}
    dishList.dishName = disVal[num].li[dis].commodity;
    dishList.dishNum = disVal[num].li[dis].buyNum;
    dishList.dishPrice = disVal[num].li[dis].dishPrice * dishList.dishNum;
    dishList.dishSp = 0;
    postData.dishList.push(dishList);

    this.setData({
      menuRight:disVal,
      postData: postData
    }, function () {
      // this is setData callback
    })
  },
  // 点击增加购买数量
  buyAdd:function(e){
    var dis = e.currentTarget.dataset.dis;
    var num = this.data.leftNum;
    var disVal = this.data.menuRight;
    disVal[num].li[dis].buyNum += 1;
    var postData = this.data.postData;
    postData.totalNum += 1;
    this.setData({
      menuRight: disVal,
      postData: postData
    }, function () {
      // this is setData callback
    })
  },
  // 点击减少购买数量
  buyDel: function (e) {
    var dis = e.currentTarget.dataset.dis;
    var num = this.data.leftNum;
    var disVal = this.data.menuRight;
    disVal[num].li[dis].buyNum -= 1;
    var postData = this.data.postData;
    postData.totalNum -= 1;
    if (disVal[num].li[dis].buyNum <= 0) {
      disVal[num].li[dis].buyNum = 0;
      disVal[num].li[dis].display = 0;
      this.setData({
        menuRight: disVal,
        postData: postData
      })
    }else{
      this.setData({
        menuRight: disVal,
        postData: postData
      }, function () {

      })
    }
  },
  // 点击选规格弹出弹窗
  specTap:function(e){
    var dis = e.currentTarget.dataset.dis;
    var num = this.data.leftNum;
    var disVal = this.data.menuRight;
    this.setData({
      specPopup:1,
      rightNum: dis
    }, function () {

    })
  },
  // 点击规格种类
  specChoiseTap:function(e){
    var dis = e.currentTarget.dataset.dis;
    var numR = this.data.rightNum;
    var num = this.data.leftNum;
    var disVal = this.data.menuRight;
    for (var i = 0; i < disVal[num].li[numR].specArr[0].spec.length;i++){
      disVal[num].li[numR].specArr[0].spec[i].specStyle = 0;
    }
    disVal[num].li[numR].specArr[0].spec[dis].specStyle = 1;
    this.setData({
      menuRight: disVal,
      specNum: dis
    }, function () {

    })
  },
  // 点击弹窗的加入购物车
  addToCar:function(e){
    // var dis = e.currentTarget.dataset.dis;
    var numSpec = this.data.specNum
    var numR = this.data.rightNum;
    var num = this.data.leftNum;
    var disVal = this.data.menuRight;
    disVal[num].li[numR].specArr[0].spec[numSpec].specAdd = 1;
    disVal[num].li[numR].specArr[0].spec[numSpec].specChoise += 1;
    disVal[num].li[numR].buyNum += 1;
    var postData = this.data.postData;
    postData.totalNum += 1;
    this.setData({
      menuRight: disVal,
      postData: postData
    }, function () {

    })
  },
  // 点击弹窗的增加购买数量
  buyAddPop:function(e){
    var numSpec = this.data.specNum
    var numR = this.data.rightNum;
    var num = this.data.leftNum;
    var disVal = this.data.menuRight;
    disVal[num].li[numR].specArr[0].spec[numSpec].specChoise += 1;
    disVal[num].li[numR].buyNum += 1;
    var postData = this.data.postData;
    postData.totalNum += 1;
    this.setData({
      menuRight: disVal,
      postData: postData
    }, function () {

    })
  },
  // 点击弹窗的减少购买数量
  buyDelPop:function(e){
    var numSpec = this.data.specNum
    var numR = this.data.rightNum;
    var num = this.data.leftNum;
    var disVal = this.data.menuRight;
    disVal[num].li[numR].specArr[0].spec[numSpec].specChoise -= 1;
    disVal[num].li[numR].buyNum -= 1;
    var postData = this.data.postData;
    postData.totalNum -= 1;
    if (disVal[num].li[numR].specArr[0].spec[numSpec].specChoise <= 0){
      disVal[num].li[numR].specArr[0].spec[numSpec].specAdd = 0;
      disVal[num].li[numR].specArr[0].spec[numSpec].specChoise = 0;
      this.setData({
        menuRight: disVal,
        postData: postData
      }, function () {

      })
    }else{
      this.setData({
        menuRight: disVal,
        postData: postData
      }, function () {

      })
    }
  },
  // 购物车菜品数量增加按钮
  carAddTap:function(e){

  },
  // 购物车菜品数量减少按钮
  carDelTap:function(e){

  },
  // 点击关闭规格弹窗
  closePopTap:function(e){
    this.setData({
      specPopup: 0
    }, function () {

    })
  },
  // 点击展示购物车弹窗
  carPopShow:function(e){
    if (this.data.postData.totalNum > 0){
      var carPopup = this.data.carPopup
      this.setData({
        carPopup: !carPopup
      })
    }
  },
  // 点击关闭购物车弹窗
  carPopHide:function(e){
    this.setData({
      carPopup: 0
    })
  },
  // 点击左下菜单展示菜单列表
  showMenuTap:function(e){
    this.setData({
      menuStyle: 1
    })
  },
  // 点击左下菜单关闭菜单列表
  hiddenMenu:function(e){
    this.setData({
      menuStyle: 0
    })
  },
  //点击清空购物车
  deleteAll:function(e){
    var that = this;
    wx.showModal({
      title: '是否确认清空购物车？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          var postData = that.data.postData;
          postData.totalNum = 0;
          postData.dishList = [];
          that.setData({
            postData: postData,
            carPopup: 0
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //点击选好了
  chooseOver:function(e){
    wx.navigateTo({
      url: '../main/payment/payment',
    })
  },
  customData: {
    hi: 'MINA'
  }
})
